package com.biz.primus.ms.depot.component.aspectj;

import com.biz.primus.model.depot.vo.IncrDepotReqVo;
import com.biz.primus.model.depot.vo.IncrEmpReqVo;
import com.biz.primus.ms.depot.service.DepotChangeLogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * DepotChangeAspect
 *
 * @author JKLiues
 * @date 2017年10月24日
 */
@Aspect
@Component
@Slf4j
public class DepotChangeAspect {

    private final DepotChangeLogService depotChangeLogService;

    @Autowired
    public DepotChangeAspect(DepotChangeLogService depotChangeLogService) {
        this.depotChangeLogService = depotChangeLogService;
    }

    @Pointcut("@annotation(com.biz.primus.ms.depot.component.annotation.DepotChangeMark)")
    public void depotChange() {
        log.info("门店接收ERP更新成功,触发写日志");
    }

    @Pointcut("@annotation(com.biz.primus.ms.depot.component.annotation.EmployeeChangeMark)")
    public void employeeChange() {
        log.info("门店员工接收ERP更新成功,触发写日志");
    }

    @After(value = "com.biz.primus.ms.depot.component.aspectj.DepotChangeAspect.depotChange()")
    @SuppressWarnings("unchecked")
    public void logDepotChange(JoinPoint jp) {
        try {
            Object[] args = jp.getArgs();
            List<IncrDepotReqVo> reqVo = (List<IncrDepotReqVo>) args[0];
            depotChangeLogService.saveDepotLog(reqVo);
        } catch (Exception e) {
            log.error("门店信息写日志异常", e.getMessage());
            e.printStackTrace();
        }
    }

    @After(value = "com.biz.primus.ms.depot.component.aspectj.DepotChangeAspect.employeeChange()")
    @SuppressWarnings("unchecked")
    public void logEmpChange(JoinPoint jp) {
        try {
            Object[] args = jp.getArgs();
            List<IncrEmpReqVo> reqVo = (List<IncrEmpReqVo>) args[0];
            depotChangeLogService.saveEmpLog(reqVo);
        } catch (Exception e) {
            log.error("员工更新写日志异常", e.getMessage());
            e.printStackTrace();
        }
    }
}